<template>
  <cv-dialog-form
    v-model:show="show"
    title="编辑分类"
    @confirm="addDataService"
  >
    <cv-form :config="form" :data="detail"> </cv-form>
  </cv-dialog-form>
</template>

<script setup lang="ts">
import type { FormConfig } from '@/packages/components'
import { addReq, queryReq, updateReq } from '@/utils/http/code'

import { cloneFnJSON, useVModels } from '@vueuse/core'

import {
  getShopGoodsCategoryCascader,
  ShopGoodsCategoryCascader,
  addShopGoodsCategory,
  AddShopGoodsCategory,
  ShopGoodsCategory,
  updateShopGoodsCategory,
  UpdateShopGoodsCategory,
} from '@/api/goods/admin-catrgory'
import { PropType } from 'vue'
import { error } from '@/utils/http/message'

const emit = defineEmits(['update:show', 'refreshTable'])
const props = defineProps({
  detail: {
    type: Object as PropType<ShopGoodsCategory>,
    default: () => {
      return {}
    },
  },
  show: {
    type: Boolean,
    default: false,
  },
})
//const { } = toRefs(props);

const { show } = useVModels(props)

/**
 * 更新商品分类数据
 * @param data
 * @param close
 */
const addDataService = async (data: ShopGoodsCategory, close: Function) => {
  console.log(data, '更新数据')

  const res = await updateShopGoodsCategory(formData(data))
  updateReq(
    res,
    () => {
      emit('refreshTable')
      close()
    },
    () => {
      error()
    }
  )

  //关闭弹窗
  //close()
}

const formData = (data: ShopGoodsCategory) => {
  const obj: UpdateShopGoodsCategory = {
    id: data.id,
    categoryName: data.categoryName,
    image: data.image,
    sort: data.sort,
    state: data.state,
    homeReferral: data.homeReferral,
  }
  return obj
}

/**
 *数据
 */
const form = reactive<FormConfig<AddShopGoodsCategory>>({
  items: [
    {
      type: 'input',
      prop: 'categoryName',
      label: '分类名称',
      rules: {
        required: true,
        type: 'string',
        max: 10,
      },
    },

    {
      type: 'image-upload',
      prop: 'image',
      label: '图片',
      upload: {
        type: 'image',
        multiple: false, //false单文件上传,true多文件上传
      },
    },

    {
      type: 'input-number',
      prop: 'sort',
      label: '分类排序',
    },

    {
      type: 'switch',
      prop: 'state',
      label: '分类状态',
      rules: { required: false },
    },
    {
      type: 'switch',
      prop: 'homeReferral',
      label: '首页推荐',
      rules: { required: false },
    },
  ],
})
</script>
<style lang="scss" scoped></style>
